﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 脚本功能：菜单页面 显示相关的 控制
/// 添加对象：TitlePosShowCtr
/// </summary>

public class MenuPanelCtr : MonoBehaviour {


    #region 数据定义和unity自带方法组

    private Button mMenuCloseOrOpenBtn, mAutoBackBtn;
    private Button mMainMenuBtn, mSoundBtn, mHelpBtn, mCameraBtn;

    public event OnClickHelpBtn OnClickHelpEvent;
    public delegate void OnClickHelpBtn();

    private bool isOpen = false;
    private GameObject mMenuPanelMaskGo;
    private RectTransform mMenuPanelBg;

    GameObject PanelHelp;

    void Awake()
    {
        var Tra = GetComponentInParent<Canvas>();
        PanelHelp = TransformHelper.FindChild(Tra.transform, "PanelHelp");
        FindGo();
        BindingEvent();
    }
    void Start()
    {
        InitDataAndPos();
    }

    #endregion


    #region  私有方法组

    private void FindGo()
    {
        if (mMenuCloseOrOpenBtn == null)
        {
            mMenuCloseOrOpenBtn = TransformHelper.FindChild(transform, "MenuCloseOrOpenBtn").GetComponent<Button>();
        }

        if (mAutoBackBtn == null)
        {
            mAutoBackBtn = TransformHelper.FindChild(transform, "AutoBackBtn").GetComponent<Button>();
        }

        if (mMainMenuBtn == null)
        {
            mMainMenuBtn = TransformHelper.FindChild(transform, "MainMenuBtn").GetComponent<Button>();
        }

        if (mSoundBtn == null)
        {
            mSoundBtn = TransformHelper.FindChild(transform, "SoundBtn").GetComponent<Button>();
        }

        if (mHelpBtn == null)
        {
            mHelpBtn = TransformHelper.FindChild(transform, "HelpBtn").GetComponent<Button>();
        }

        if (mCameraBtn == null)
        {
            mCameraBtn = TransformHelper.FindChild(transform, "CameraBtn").GetComponent<Button>();
        }

    }

    private void BindingEvent()
    {
        if (mMenuPanelMaskGo == null)
        {
            mMenuPanelMaskGo = TransformHelper.FindChild(transform, "MenuPanelMask");
        }
        if (mMenuPanelBg == null)
        {
            mMenuPanelBg = (TransformHelper.FindChild(transform, "MenuPanelBg").transform) as RectTransform;
        }

        if (mMenuCloseOrOpenBtn != null)
        {
            mMenuCloseOrOpenBtn.onClick.AddListener(delegate () { OnClickMenuShowOrHide(); });
        }

        if (mAutoBackBtn != null)
        {
            mAutoBackBtn.onClick.AddListener(delegate () { OnClickMenuShowOrHide(); });
        }

        if (mMainMenuBtn != null)
        {
            mMainMenuBtn.onClick.AddListener(delegate () 
            {
                LoadingSceneForDll.LoadSubContent();
                //print("主菜单 按钮");
            });
        }

        if (mSoundBtn != null)
        {
            mSoundBtn.onClick.AddListener(delegate () { print("声音开关"); });
        }
        if (mHelpBtn != null)
        {
            mHelpBtn.onClick.AddListener(OpenHelp);
        }
        if (mCameraBtn != null)
        {
            mCameraBtn.onClick.AddListener(delegate () { print("摄像机 按钮"); });
        }
    }

    private void InitDataAndPos()
    {
        mAutoBackBtn.gameObject.SetActive(false);
        mMenuPanelBg.gameObject.SetActive(false);
        mMenuPanelMaskGo.SetActive(false);
    }

    void OpenHelp()
    {
        if (PanelHelp != null)
        {
            PanelHelp.gameObject.SetActive(true);
            PanelHelp.SendMessage("OnHelpClick");
        }
        else
        {
            OnClickHelpEvent(); 
        }
    }
    private void OnClickMenuShowOrHide()
    {
        isOpen = !isOpen;
        mAutoBackBtn.gameObject.SetActive(isOpen);
        if (isOpen)
        {
            //print("打开，播放动画");
            mMenuPanelMaskGo.SetActive(true);
            mMenuPanelBg.gameObject.SetActive(true);
        }
        else
        {
            //print("关闭，播放动画");
            mMenuPanelBg.gameObject.SetActive(false);
            mMenuPanelMaskGo.SetActive(false);
        }
    }


    #endregion



    #region  公有方法组

    #endregion

}
